package com.greenhou.config;

import com.greenhou.io.Resources;
import com.greenhou.pojo.Configuration;
import com.greenhou.pojo.MappedStatement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @ClassName XmlConfigBuilder
 * @Description 解析xml
 * @Author YR
 * @Date 2020/6/8 22:22
 * @Version 1.0
 */
public class XmlConfigBuilder {
    //数据库配置信息
    private Configuration configuration;

    public XmlConfigBuilder() {
        this.configuration = new Configuration();
    }

    /**
     * @Description 将配置文件进行解析并封装到Configuration容器里边
     * @Params [in]
     * @return com.greenhou.pojo.Configuration
     * @Author yeren
     * @Date 2020/6/8 22:31
     */
    public Configuration parseConfig(InputStream in) throws DocumentException, PropertyVetoException {
        Document read = new SAXReader().read(in);
        //获取到了sqlMapConfig.xml 的跟<configuration>
        Element rootElement = read.getRootElement();
        List<Element> property = rootElement.selectNodes("//property");
        Properties properties=new Properties();
        if(property!=null && property.size()>0){
            for (Element element:property) {
                properties.setProperty(element.attributeValue("name"),element.attributeValue("value"));
            }

            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
            comboPooledDataSource.setDriverClass(properties.getProperty("jdbcClass"));
            comboPooledDataSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
            comboPooledDataSource.setUser(properties.getProperty("username"));
            comboPooledDataSource.setPassword(properties.getProperty("password"));
            this.configuration.setDataSource(comboPooledDataSource);

        }else{
            throw new DocumentException("未找到 dataSource > property node ，请检查sqlMapConfig.xml");
        }

        //开始解析mapper.xml
        Node mappers_node = rootElement.selectSingleNode("mappers");
        if(mappers_node!=null){
            List<Element> mappers = mappers_node.selectNodes("mapper");
            if(mappers!=null && mappers.size()>0){
                for(Element element:mappers){
                    InputStream inputStream=Resources.getResourceAsStream(element.attributeValue("resource"));
                    XMLMapperBuilder xmlMapperBuilder=new XMLMapperBuilder(this.configuration);
                    xmlMapperBuilder.parse(inputStream);
                }
            }else{
                throw new DocumentException("未找到 mappers -> mapper node ，请检查sqlMapConfig.xml");
            }
        }else{
            throw new DocumentException("未找到 mappers node ，请检查sqlMapConfig.xml");
        }
        return this.configuration;
    }
}
